<template>
	<view>
		<navbar :autoBack='false'></navbar>
		<view class="con u-flex u-flex-column u-flex-items-center">
			<!-- <view class="logo">

			</view> -->
			<image src="/static/logo.png" class="logo" mode=""></image>
			<view class="title">
				
			</view>
			<!-- 密码登录 -->
			<view class="form" v-if="ispwd">
				<view class="inpbox">
					<image src="/static/login/phone.png" class="inpbox__phone" mode=""></image>
					<input class="inpbox__inp" v-model="form.account" type="number" placeholder="请输入账号" />
				</view>
				<view class="inpbox">
					<image src="/static/login/pws.png" class="inpbox__pws" mode=""></image>
					<input class="inpbox__inp" type="password" v-model="form.password" placeholder="请输入密码" />
				</view>
			</view>
			<!-- 验证码登录 -->
			<view class="form" v-else>
				<view class="inpbox">
					<image src="/static/login/phone.png" class="inpbox__phone" mode=""></image>
					<input class="inpbox__inp" type="number" placeholder="请输入账号" v-model="form1.mobile" />
				</view>
				<view class="inpbox">
					<image src="/static/login/yzm.png" class="inpbox__yzm" mode=""></image>
					<input class="inpbox__inp" type="text" placeholder="请输入验证码" v-model="form1.captcha" />
					<u-button color="#3262FD" @tap="getCode"
						:customStyle='{width:"200rpx",height:"84rpx",borderRadius:"15rpx",marginRight:"10rpx",fontSize:"24rpx"}'>{{tips}}</u-button>
				</view>
			</view>
			<u-button text="登录" color="#3262FD" size="large"
				:customStyle='{width:"606rpx",height:"111rpx",borderRadius:"15rpx",marginTop:"30rpx"}' @click="login"></u-button>
			<view class="tips">
				<view class="tips__item" v-if="ispwd" @click="ispwd=false">
					验证码登录
				</view>
				<view class="tips__item" v-else @click="ispwd=true">
					密码登录
				</view>
				<view class="tips__item" @click="navto('/pages/index/reg')">
					注册账号
				</view>
			</view>
			<view class="tips1">
				-- 微信登录 --
			</view>
			<image src="/static/login/wx.png" class="wx" mode=""></image>
		</view>
		<u-code :keep-running='true' change-text='Xs重新获取' :seconds="seconds" ref="uCode"
			@change="codeChange"></u-code>
	</view>
</template>

<script>
	import user from '@/api/_user.js'
	export default {
		
		data() {
			return {
				tips: '',
				// refCode: null,
				seconds: 60,
				ispwd: true,
				form:{
					account:'',
					password:''
				},
				form1:{
					mobile:'',
					captcha:''
				}
			};
		},
		onReady() {
			// 注意这里不能将一个组件赋值给data的一个变量，否则在微信小程序会
			// 造成循环引用而报错，如果你想这样做，请在非data中定义refCode变量
			this.refCode = this.$refs.uCode;
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			login(){
				if(this.ispwd){
					if(!this.form.account){
						this.$u.toast('请输入账号')
						return
					}
					if(!this.form.password){
						this.$u.toast('请输入密码')
						return
					}
					user.login(this.form).then(res=>{
						if(res.data.code==1){
							uni.$u.toast('登录成功');
							uni.setStorageSync('88ftoken',res.data.data.userinfo.token)
							setTimeout(()=>{
								uni.switchTab({
									url:'/pages/tabbr/personal'
								})
							},1000)
						}
					})
				}else{
					if(!this.form1.mobile){
						this.$u.toast('请输入手机号')
						return
					}
					if(!this.form1.captcha){
						this.$u.toast('请输入验证码')
						return
					}
					user.mobilelogin(this.form1).then(res=>{
						if(res.data.code==1){
							uni.$u.toast('登录成功');
							uni.setStorageSync('88ftoken',res.data.data.userinfo.token)
							setTimeout(()=>{
								uni.switchTab({
									url:'/pages/tabbr/personal'
								})
							},1000)
						}
					})
				}
				
				
			},
			getCode() {
				if(!this.form1.mobile){
					uni.$u.toast('请输入手机号');
					return
				}
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					user.send({mobile:this.form1.mobile,event:'mobilelogin'}).then(res=>{
						uni.hideLoading();
						if(res.data.code==1){
							uni.$u.toast('验证码已发送');
							this.$refs.uCode.start();
						}
					})
					// setTimeout(() => {
					// 	uni.hideLoading();
					// 	// 这里此提示会被this.start()方法中的提示覆盖
					// 	uni.$u.toast('验证码已发送');
					// 	// 通知验证码组件内部开始倒计时
					// 	this.$refs.uCode.start();
					// }, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #F0F3F7;
	}
	.con {
		padding-top: 100rpx;
		position: relative;
		padding-bottom: 150rpx;
	}

	.logo {
		width: 224rpx;
		height: 225rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 7rpx 40rpx 0rpx rgba(72, 139, 254, 0.18);
		border-radius: 90rpx;
	}

	.title {
		width: 326rpx;
		height: 39rpx;
		font-family: PingFang SC;
		font-weight: bold;
		font-size: 41rpx;
		color: #1E1E1E;
		line-height: 41rpx;
		margin: 70rpx 0 79rpx;
	}

	.inpbox {
		width: 606rpx;
		height: 107rpx;
		background: #F6F9FF;
		border-radius: 15rpx;
		display: flex;
		align-items: center;
		margin-bottom: 33rpx;

		&__pws {
			width: 32rpx;
			height: 37rpx;
			margin-left: 46rpx;
			margin-right: 28rpx;
		}

		&__phone {
			width: 28rpx;
			height: 35rpx;
			margin: 0 30rpx 0 46rpx;
		}

		&__yzm {
			width: 28rpx;
			height: 33rpx;
			margin: 0 30rpx 0 48rpx;
		}

		&__inp {
			flex: 1;
			font-family: PingFang SC;
			font-size: 28rpx;
			color: #333;
		}

		.uni-input-placeholder {
			color: #A1A4AA;
			font-family: PingFang SC;
			font-size: 28rpx;
		}
	}

	.tips {
		width: 606rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-family: PingFang SC;
		font-weight: 500;
		font-size: 26rpx;
		color: #A9ADB8;
		line-height: 26rpx;
		margin-top: 34rpx;
		margin-bottom: 100rpx;
	}

	.tips1 {
		font-family: PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		color: #A1A4AA;
		line-height: 28rpx;
	}

	.wx {
		width: 112rpx;
		height: 112rpx;
		margin-top: 29rpx;
	}

	.u-button--normal {
		padding: 0;
	}
</style>